/*
 * CreateDomXml.java
 *
 * Created on 26 ตุลาคม 2550, 20:16 น.
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package anything;

import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
/**
 *
 * @author User
 */
public class CreateDomXml {
  
  /** Creates a new instance of CreateDomXml */
  public CreateDomXml() {
  }
  
  public static void main(String[] args) {
    try {
      //Create instance of DocumentBuilderFactory
      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
      //Get the DocumentBuilder
      DocumentBuilder docBuilder = factory.newDocumentBuilder();
      //Create blank DOM Document
      Document doc = docBuilder.newDocument();
      
      //Create the root element
      Element root = doc.createElement("root");
      //Add it to the xml tree
      doc.appendChild(root);
      
      //Create a comment
      Comment comment = doc.createComment("This is comment");
      //Add in the root element
      root.appendChild(comment);
      
      //Create child element
      Element childElement = doc.createElement("Child");
      //Add the attribute to the child
      childElement.setAttribute("attribute1", "The value of Attribute 1");
      root.appendChild(childElement);
      
      TransformerFactory transFactory = TransformerFactory.newInstance();
      Transformer aTrans = transFactory.newTransformer();
      
      Source src = new DOMSource(doc);
      Result dest = new StreamResult(System.out);
      aTrans.transform(src, dest);
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }
}
